<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="state.loginForm"
      :rules="state.loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">登录</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon name="user" />
        </span>
        <el-input
          ref="username"
          v-model="state.loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon name="password" icon-class="password" />
        </span>
        <el-input
          v-model="state.loginForm.password"
          placeholder="Password"
          tabindex="2"
          auto-complete="on"
          show-password
          @keyup.enter="handleLogin"
        />
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click="handleLogin"
        >登录</el-button
      >
      <div class="tip">提示：采用不同测试账号登录加载不同权限；</div>
      <div class="tip">账号：'admin'/ 'guest',密码'test'</div>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const state = reactive({
  loginForm: {
    username: '',
    password: 'test'
  },
  loginRules: {
    username: [
      {
        required: true,
        trigger: 'blur',
        validator: (rule, value, callback) => {
          if (!value) {
            callback(new Error('请输入登录账号'))
          } else {
            callback()
          }
        }
      }
    ],
    password: [
      {
        required: true,
        trigger: 'blur',
        validator: (rule, value, callback) => {
          if (value !== 'test') {
            callback(new Error('密码错误'))
          } else {
            callback()
          }
        }
      }
    ]
  }
})
const store = useStore()
const loginForm = ref(null)
const loading = ref(false)

function handleLogin() {
  loginForm.value.validate((valid) => {
    if (valid) {
      loading.value = true
      store
        .dispatch('user/login', state.loginForm)
        .then(() => {
          loading.value = false
          router.push({ path: '/' })
        })
        .catch(() => {
          loading.value = false
          ElMessage.error('用户名或密码错误')
        })
    }
  })
}
</script>

<style lang="scss" scoped>
$light_gray: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #283443;
  overflow: hidden;
  ::v-deep(.el-input) {
    width: calc(100% - 36px);
    height: 47px;
    display: inline-block;
    caret-color: $light_gray;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #283443 inset !important;
        -webkit-text-fill-color: $light_gray !important;
      }
    }
    .el-input__suffix {
      line-height: 47px;
    }
  }
  ::v-deep(.el-form-item) {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #889aa4;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .tip {
    color: #fff;
  }
}
</style>
